package com.tianqu.tms.www.statistics.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.www.statistics.dao.TmsTransferAreaDayCountDao;
import com.tianqu.tms.www.statistics.dao.TmsTransferDayCountDao;
import com.tianqu.tms.www.statistics.dao.TmsTransferMonthCountDao;
import com.tianqu.tms.www.statistics.dto.ComboDto;
import com.tianqu.tms.www.statistics.dto.TradeTransferDto;
import com.tianqu.tms.www.statistics.dto.TradeTransferPreMonthDto;
import com.tianqu.tms.www.statistics.dto.TradeTransferYesteryearDto;

public class TradeTransferService {

	private Logger LOGGER = Logger.getLogger(TradeTransferService.class);
	
	/**
	 * 取得统计累计天数和統計開始日
	 * @return
	 * @throws TmsDaoException
	 */
	public TradeTransferDto getCumulativeDays() throws TmsDaoException{
	
		TmsTransferDayCountDao tmsTransferDayCountDao = new TmsTransferDayCountDao();
		
		TradeTransferDto tradeTransferDto = new TradeTransferDto();
		try {
			// 取得二、累计交易过户总数 & 三、累计交易过户总价
			tradeTransferDto = tmsTransferDayCountDao.getAccumulativeTransaction();
			
			// 统计开始日期
			Calendar dayc1 = new GregorianCalendar();
			
			String strDate = tmsTransferDayCountDao.getStartCountDate();
			Date firstDate = DateUtil.string102ToDate(strDate);
			
			dayc1.setTime(firstDate);
			
			// 当前时间
			Calendar dayc2 = new GregorianCalendar();
			dayc2.setTime(new Date());
			
			int days = DateUtil.getDaysBetween(dayc1, dayc2);
			
			// 累计天数
			tradeTransferDto.setCountDays(days);
			
			// 统计开始日期
			tradeTransferDto.setCountDate(DateUtil.DateToString11(firstDate));
		} catch (Exception e) {
			throw new TmsDaoException(e.toString());
		}
		
		return tradeTransferDto;
	}
	
//	/**
//	 * 取得二、累计交易过户总数 & 三、累计交易过户总价
//	 * @return
//	 * @throws TmsDaoException
//	 */
//	public TradeTransferDto getAccumulativeTransaction() throws TmsDaoException{
//	
//		TmsTransferDayCountDao tmsTransferDayCountDao = new TmsTransferDayCountDao();
//		
//		TradeTransferDto tradeTransferDto = null;
//		try {
//			
//			tradeTransferDto = tmsTransferDayCountDao.getAccumulativeTransaction();
//			
//		} catch (Exception e) {
//			throw new TmsDaoException(e.toString());
//		}
//		
//		return tradeTransferDto;
//	}
	
	/**
	 * 取得今天交易过户量 &今天商户交易过户量&今天社会交易过户量
	 * @return
	 * @throws TmsDaoException
	 */
	public TradeTransferDto getTodayTransaction() throws TmsDaoException{
	
		TmsTransferDayCountDao tmsTransferDayCountDao = new TmsTransferDayCountDao();
		
		TradeTransferDto tradeTransferDto = null;
		try {
			
			tradeTransferDto = tmsTransferDayCountDao.getTodayTransaction();
			
		} catch (Exception e) {
			throw new TmsDaoException(e.toString());
		}
		
		return tradeTransferDto;
	}
	
//	/**
//	 * 取得当月新增数据 交易过户量（transferNum） 商户业务量（tranType11） 社会业务量（tranType12）
//	 * @return
//	 * @throws TmsDaoException
//	 */
//	public TradeTransferDto getMonthIncreaseInfo() throws TmsDaoException{
//	
//		TmsTransferDayCountDao tmsTransferDayCountDao = new TmsTransferDayCountDao();
//		
//		TradeTransferDto tradeTransferDto = null;
//		try {
//			
//			tradeTransferDto = tmsTransferDayCountDao.getMonthIncreaseInfo();
//			
//		} catch (Exception e) {
//			throw new TmsDaoException(e.toString());
//		}
//		
//		return tradeTransferDto;
//	}
	
	/**
	 * 当月新增同比 & 当月新增环比
	 * @return
	 * @throws TmsDaoException
	 */
	public TradeTransferDto getCurrentMonthSameRate() throws TmsDaoException{
	
		TmsTransferDayCountDao tmsTransferDayCountDao = new TmsTransferDayCountDao();
		
		TradeTransferDto tradeTransferDto = null;
		try {
			
			// 上一年度当前月份交易过户总量
			TradeTransferYesteryearDto tradeTransferYesteryearDto = tmsTransferDayCountDao.getYesteryearTransaction();
			
			Calendar cal = Calendar.getInstance();
			int year = cal.get(Calendar.YEAR);
			int month = cal.get(Calendar.MONTH )+1;
			int day =cal.get(Calendar.DAY_OF_MONTH);
			
			// 上一年月份总天数
			int yesteryearMonthDays = DateUtil.getDaysByYearMonth(year-1, month);
			
			LOGGER.info("上一年度当前月份交易过户总量 getTransferNumYMonthTotal:"+tradeTransferYesteryearDto.getTransferNumYMonthTotal());
			LOGGER.info("上一年度当前月份交易过户总量 getTranType11YMonthTotal:"+tradeTransferYesteryearDto.getTranType11YMonthTotal());
			LOGGER.info("上一年度当前月份交易过户总量 getTranType12YMonthTotal:"+tradeTransferYesteryearDto.getTranType12YMonthTotal());
			
			// 上一年度同期累计交易过户数量 = （上一年度当前月份交易过户总量/月份总天数）*本月到系统当前时间的天数；
			BigDecimal yTransferNum = tradeTransferYesteryearDto.getTransferNumYMonthTotal().multiply(new BigDecimal(day)).divide(new BigDecimal(yesteryearMonthDays),4,BigDecimal.ROUND_HALF_DOWN);
			BigDecimal yTranType11 = tradeTransferYesteryearDto.getTranType11YMonthTotal().multiply(new BigDecimal(day)).divide(new BigDecimal(yesteryearMonthDays),4,BigDecimal.ROUND_HALF_DOWN);
			BigDecimal yTranType12 = tradeTransferYesteryearDto.getTranType12YMonthTotal().multiply(new BigDecimal(day)).divide(new BigDecimal(yesteryearMonthDays),4,BigDecimal.ROUND_HALF_DOWN);
			
			// 当月新增同比，当月累计到系统当前日期与上一年度同期相比，即（本月累计交易过户数量 - 上一年度同期累计交易过户数量）/上一年度同期累计交易过户数量*100%，保留一位小数，四舍五入；
			tradeTransferDto = tmsTransferDayCountDao.getMonthIncreaseInfo();
			
			BigDecimal sameTransferNum = BigDecimal.ZERO;
			BigDecimal sameTranType11 = BigDecimal.ZERO;
			BigDecimal sameTranType12 = BigDecimal.ZERO;
			
			if(BigDecimal.ZERO.compareTo(yTransferNum) != 0) {
				
				sameTransferNum = tradeTransferDto.getTransferNumMonthTotal().subtract(yTransferNum).divide(yTransferNum,4,BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100));
			}
			
			if(BigDecimal.ZERO.compareTo(yTranType11) != 0) {
				
				sameTranType11 = tradeTransferDto.getTranType11MonthTotal().subtract(yTranType11).divide(yTranType11,4,BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100));
			}
			
			if(BigDecimal.ZERO.compareTo(yTranType12) != 0) {
				
				sameTranType12 =tradeTransferDto.getTranType12MonthTotal().subtract(yTranType12).divide(yTranType12,4,BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100));
			}
			
			java.text.DecimalFormat myformat=new java.text.DecimalFormat("0.0");
			
			tradeTransferDto.setSameTransferNum(myformat.format(sameTransferNum)+"%");
			tradeTransferDto.setSameTranType11(myformat.format(sameTranType11)+"%");
			tradeTransferDto.setSameTranType12(myformat.format(sameTranType12)+"%");
			
			// 当月新增环比，当月累计到系统当前日期与上一月份同期相比
			TradeTransferPreMonthDto tradeTransferPreMonthDto = tmsTransferDayCountDao.getPreMonthTransaction();
			
			BigDecimal circleTransferNum = BigDecimal.ZERO;
			BigDecimal circleTranType11 = BigDecimal.ZERO;
			BigDecimal circleTranType12 = BigDecimal.ZERO;
			
			BigDecimal preMonthTransferNum = tradeTransferPreMonthDto.getTransferNumPreMonthTotal();
			BigDecimal preMonthTranType11 = tradeTransferPreMonthDto.getTranType11PreMonthTotal();
			BigDecimal preMonthTranType12 = tradeTransferPreMonthDto.getTranType12PreMonthTotal();
			
			// 即（本月累计交易过户数量 - 上一月份同期累计交易过户数量）/上一月份同期累计交易过户数量*100%，保留一位小数，四舍五入
			
			if(BigDecimal.ZERO.compareTo(preMonthTransferNum) != 0) {
				circleTransferNum = tradeTransferDto.getTransferNumMonthTotal().subtract(preMonthTransferNum).divide(preMonthTransferNum,4,BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100));
			}
			
			if(BigDecimal.ZERO.compareTo(preMonthTranType11) != 0) {
				circleTranType11 = tradeTransferDto.getTranType11MonthTotal().subtract(preMonthTranType11).divide(preMonthTranType11,4,BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100));
			}
			
			if(BigDecimal.ZERO.compareTo(preMonthTranType12) != 0) {
				circleTranType12 = tradeTransferDto.getTranType12MonthTotal().subtract(preMonthTranType12).divide(preMonthTranType12,4,BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100));
			}
			
			tradeTransferDto.setCircleTransferNum(myformat.format(circleTransferNum)+"%");
			tradeTransferDto.setCircleTranType11(myformat.format(circleTranType11)+"%");
			tradeTransferDto.setCircleTranType12(myformat.format(circleTranType12)+"%");
		} catch (Exception e) {
			throw new TmsDaoException(e.toString());
		}
		
		return tradeTransferDto;
	}

	/**
	 * 十、国产VS进口 
	 * @return
	 * @throws TmsDaoException
	 */
	public TradeTransferDto getDomesticVSImport() throws TmsDaoException{
	
		TmsTransferDayCountDao tmsTransferDayCountDao = new TmsTransferDayCountDao();
		
		List<ComboDto> domesticVSImport = new ArrayList<ComboDto>();
		
		TradeTransferDto tradeTransferDto = null; 
		try {
			
			tradeTransferDto = tmsTransferDayCountDao.getPriceInfo();
			
			ComboDto dto1 = new ComboDto();
			dto1.setName("国产");
			dto1.setValue(String.valueOf(tradeTransferDto.getTranType21Total90()));
			
			ComboDto dto2 = new ComboDto();
			dto2.setName("进口");
			dto2.setValue(String.valueOf(tradeTransferDto.getTranType22Total90()));
			
			domesticVSImport.add(dto1);
			domesticVSImport.add(dto2);
			
			tradeTransferDto.setDomesticVSImport(domesticVSImport);
		} catch (Exception e) {
			throw new TmsDaoException(e.toString());
		}
		
		return tradeTransferDto;
	}
	
	/**
	 * 十一、个人VS企业 
	 * @return
	 * @throws TmsDaoException
	 */
	public TradeTransferDto getPersonVSCompany() throws TmsDaoException{
	
		TmsTransferDayCountDao tmsTransferDayCountDao = new TmsTransferDayCountDao();
		
		List<ComboDto> personVSCompany = new ArrayList<ComboDto>();
		
		TradeTransferDto tradeTransferDto = null; 
		try {
			
			tradeTransferDto = tmsTransferDayCountDao.getPriceInfo();
			
			ComboDto dto1 = new ComboDto();
			dto1.setName("个人");
			dto1.setValue(String.valueOf(tradeTransferDto.getTranType31Total90()));
			
			ComboDto dto2 = new ComboDto();
			dto2.setName("企业");
			dto2.setValue(String.valueOf(tradeTransferDto.getTranType32Total90()));
			
			personVSCompany.add(dto1);
			personVSCompany.add(dto2);
			
			tradeTransferDto.setPersonVSCompany(personVSCompany);
		} catch (Exception e) {
			throw new TmsDaoException(e.toString());
		}
		
		return tradeTransferDto;
	}
	
	/**
	 * 十二、黄牌VS蓝牌
	 * @return
	 * @throws TmsDaoException
	 */
	public TradeTransferDto getYellowVSBlue() throws TmsDaoException{
	
		TmsTransferDayCountDao tmsTransferDayCountDao = new TmsTransferDayCountDao();
		
		List<ComboDto> yellowVSBlue = new ArrayList<ComboDto>();
		
		TradeTransferDto tradeTransferDto = null; 
		try {
			
			tradeTransferDto = tmsTransferDayCountDao.getPriceInfo();
			
			ComboDto dto1 = new ComboDto();
			dto1.setName("黄牌");
			dto1.setValue(String.valueOf(tradeTransferDto.getTranType41Total90()));
			
			ComboDto dto2 = new ComboDto();
			dto2.setName("蓝牌");
			dto2.setValue(String.valueOf(tradeTransferDto.getTranType42Total90()));
			
			yellowVSBlue.add(dto1);
			yellowVSBlue.add(dto2);
			
			tradeTransferDto.setYellowVSBlue(yellowVSBlue);
		} catch (Exception e) {
			throw new TmsDaoException(e.toString());
		}
		
		return tradeTransferDto;
	}
	
	/**
	 * 八、价格分布
	 * @return
	 * @throws TmsDaoException
	 */
	public TradeTransferDto getPriceDistribute() throws TmsDaoException{
	
		TmsTransferDayCountDao tmsTransferDayCountDao = new TmsTransferDayCountDao();
		
		List<ComboDto> priceDistribute = new ArrayList<ComboDto>();
		
		TradeTransferDto tradeTransferDto = null; 
		try {
			
			tradeTransferDto = tmsTransferDayCountDao.getPriceInfo();
			
			//5万以下
			ComboDto dto1 = new ComboDto();
			dto1.setName("5万以下");
			dto1.setValue(String.valueOf(tradeTransferDto.getUnder5()));
			//5~8万
			ComboDto dto2 = new ComboDto();
			dto2.setName("5~8万");
			dto2.setValue(String.valueOf(tradeTransferDto.getUnder58()));
			//8~10万
			ComboDto dto3 = new ComboDto();
			dto3.setName("8~10万");
			dto3.setValue(String.valueOf(tradeTransferDto.getUnder810()));
			//10~15万
			ComboDto dto4 = new ComboDto();
			dto4.setName("10~15万");
			dto4.setValue(String.valueOf(tradeTransferDto.getUnder1015()));
			//15~20万
			ComboDto dto5 = new ComboDto();
			dto5.setName("15~20万");
			dto5.setValue(String.valueOf(tradeTransferDto.getUnder1520()));
			//20~25万
			ComboDto dto6 = new ComboDto();
			dto6.setName("20~25万");
			dto6.setValue(String.valueOf(tradeTransferDto.getUnder2025()));
			// 25~40万
			ComboDto dto7 = new ComboDto();
			dto7.setName("25~40万");
			dto7.setValue(String.valueOf(tradeTransferDto.getUnder2540()));
			//40万以上
			ComboDto dto8 = new ComboDto();
			dto8.setName("40万以上");
			dto8.setValue(String.valueOf(tradeTransferDto.getUp40()));
			
			priceDistribute.add(dto1);
			priceDistribute.add(dto2);
			priceDistribute.add(dto3);
			priceDistribute.add(dto4);
			priceDistribute.add(dto5);
			priceDistribute.add(dto6);
			priceDistribute.add(dto7);
			priceDistribute.add(dto8);
			
			tradeTransferDto.setPriceDistribute(priceDistribute);
		} catch (Exception e) {
			throw new TmsDaoException(e.toString());
		}
		
		return tradeTransferDto;
	}
	
	/**
	 * 九、车龄分布
	 * @return
	 * @throws TmsDaoException
	 */
	public TradeTransferDto getCarAgeDistribute() throws TmsDaoException{
	
		TmsTransferDayCountDao tmsTransferDayCountDao = new TmsTransferDayCountDao();
		
		List<ComboDto> carAgeDistribute = new ArrayList<ComboDto>();
		
		TradeTransferDto tradeTransferDto = null; 
		try {
			
			tradeTransferDto = tmsTransferDayCountDao.getPriceInfo();
			
			//# 1年以内
			ComboDto dto1 = new ComboDto();
			dto1.setName("1年以内");
			dto1.setValue(String.valueOf(tradeTransferDto.getCarAge1()));
			//1~3年
			ComboDto dto2 = new ComboDto();
			dto2.setName("1~3年");
			dto2.setValue(String.valueOf(tradeTransferDto.getCarAge13()));
			//3~6年
			ComboDto dto3 = new ComboDto();
			dto3.setName("3~6年");
			dto3.setValue(String.valueOf(tradeTransferDto.getCarAge36()));
			//6年以上
			ComboDto dto4 = new ComboDto();
			dto4.setName("6年以上");
			dto4.setValue(String.valueOf(tradeTransferDto.getCarAge7()));
			
			carAgeDistribute.add(dto1);
			carAgeDistribute.add(dto2);
			carAgeDistribute.add(dto3);
			carAgeDistribute.add(dto4);
			
			tradeTransferDto.setCarAgeDistribute(carAgeDistribute);
		} catch (Exception e) {
			throw new TmsDaoException(e.toString());
		}
		
		return tradeTransferDto;
	}
	
	/**
	 * 取得十三、流出省TOP5
	 * @return
	 * @throws TmsDaoException
	 */
	public TradeTransferDto getTop5OfProv() throws TmsDaoException{
	
		TmsTransferAreaDayCountDao tmsTransferAreaDayCountDao = new TmsTransferAreaDayCountDao();
		
		TradeTransferDto tradeTransferDto = null;
		try {
			
			tradeTransferDto = tmsTransferAreaDayCountDao.getTop5OfProv();
			
		} catch (Exception e) {
			throw new TmsDaoException(e.toString());
		}
		
		return tradeTransferDto;
	}
	
	/**
	 * 取得十四、流出本省城市TOP5
	 * @return
	 * @throws TmsDaoException
	 */
	public TradeTransferDto getTop5OfCity() throws TmsDaoException{
	
		TmsTransferAreaDayCountDao tmsTransferAreaDayCountDao = new TmsTransferAreaDayCountDao();
		
		TradeTransferDto tradeTransferDto = null;
		try {
			
			tradeTransferDto = tmsTransferAreaDayCountDao.getTop5OfCity();
			
		} catch (Exception e) {
			throw new TmsDaoException(e.toString());
		}
		
		return tradeTransferDto;
	}
	
	/**
	 * 取得十五、最近10个月交易过户量走势图
	 * @return
	 * @throws TmsDaoException
	 */
	public TradeTransferDto getLastTenMonthsTransferNum() throws TmsDaoException{
	
		TradeTransferDto tradeTransferDto = new TradeTransferDto();
		TmsTransferMonthCountDao tmsTransferMonthCountDao = new TmsTransferMonthCountDao();
		List<ComboDto> list = new ArrayList<ComboDto>();
		try {
			list  = tmsTransferMonthCountDao.getLastMonthsTransferNumTop9();
			ComboDto dto = tmsTransferMonthCountDao.getLastMonthsTransferNumTop1();
			Collections.reverse(list);
			list.add(dto);
			
			tradeTransferDto.setNear10Month(list);
		} catch (Exception e) {
			throw new TmsDaoException(e.toString());
		}
		
		return tradeTransferDto;
	}
}
